#include "DaterEventTagWindow.h"
#include "DaterCommon.h"
#include "DaterBriefEventWindow.h"
#include "DaterBaseWindowManager.h"
#include "DaterSystem.h"
#include "DaterEventManager.h"
#include "DaterMainCalendarManagerWindow.h"
#include "DaterEventEditWindow.h"
#include "DaterEventMessageCreator.h"
#include "DaterMessageQueue.h"

EventTagWindow::EventTagWindow(int index, float yPositionStart, float yPositionEnd, Dater::Event* relatedEvent, CEGUI::Window* parent):
BaseWindow(parent),bEventWindowAttached(false)
{
	float offset = 0.01f;
	_root = CEGUI::WindowManager::getSingleton().createWindow("WindowsLook/Static_" + relatedEvent->getTagColor(),
		_parent->getName() + "/event" + CEGUI::PropertyHelper::intToString(index));

	_name = _root->getName().c_str();

	_type = "static";

	_relatedEvent = relatedEvent;

	_root->setArea(CEGUI::URect(CEGUI::UVector2(CEGUI::UDim(0,0), CEGUI::UDim(yPositionStart + offset,0)),
		CEGUI::UVector2(CEGUI::UDim(1,0), CEGUI::UDim(yPositionEnd - offset, 0))));

	_root->setText(mbcs_to_utf8(_relatedEvent->getName().c_str()));
	_root->setFont("Chinese-12");
	_root->setProperty("FrameEnabled", CEGUI::PropertyHelper::boolToString(false));

	_root->subscribeEvent(CEGUI::Window::EventMouseClick, CEGUI::Event::Subscriber(&EventTagWindow::handle_addBriefEventWindow, this));
	
	showWindow();
}

EventTagWindow::~EventTagWindow()
{
	CEGUI::WindowManager::getSingleton().destroyWindow(_root);
}

void EventTagWindow::showWindow()
{
	_parent->addChildWindow(_root);
}

void EventTagWindow::hideWindow()
{
}

bool EventTagWindow::handle_addBriefEventWindow(const CEGUI::EventArgs& args)
{
	using namespace CEGUI;

	if(!bEventWindowAttached){
		bEventWindowAttached = true;
		BriefEventWindow* briefEventWindow = new BriefEventWindow("", _relatedEvent, _root);
		BaseWindowManager::getSingleton().addBaseWindow(briefEventWindow);

		_root->subscribeEvent(CEGUI::Window::EventDeactivated, CEGUI::Event::Subscriber(&EventTagWindow::handle_destroyBriefEventWindow, this));

		WindowManager::getSingleton().getWindow("BriefEventWindow/DeleteButton")->subscribeEvent(
			PushButton::EventMouseClick, Event::Subscriber(&EventTagWindow::handle_deleteEvent, this));
		WindowManager::getSingleton().getWindow("BriefEventWindow/EditButton")->subscribeEvent(
			PushButton::EventMouseClick, Event::Subscriber(&EventTagWindow::handle_editEvent, this));
	}

	return true;
}

bool EventTagWindow::handle_destroyBriefEventWindow(const CEGUI::EventArgs& args)
{
	bEventWindowAttached = false;
	BaseWindowManager::getSingleton().destroyBaseWindow("BriefEventWindow");

	return true;
}

bool EventTagWindow::handle_deleteEvent(const CEGUI::EventArgs& args)
{
	BaseWindowManager::getSingleton().destroyBaseWindow("BriefEventWindow");
	Dater::System::getSingleton().getCalendarData()->deleteEvent(_relatedEvent);

	Dater::EventMessageCreator* eventMessageCreator = new Dater::EventMessageCreator();

	std::string message = eventMessageCreator->getDeleteEventMessage(_relatedEvent);

	delete eventMessageCreator;

	Dater::SendMessageQueue& sendMessageQueue = Dater::SendMessageQueue::getSingleton();
	if(!sendMessageQueue.isLocked())
	{
		sendMessageQueue.lock();
		sendMessageQueue.pushBufferToQueue();
		sendMessageQueue.push(message);
		sendMessageQueue.unLock();
	}else
	{
		sendMessageQueue.pushMessageToBuffer(message);
	}

	Dater::EventManager::getSingleton().destroyEvent(_relatedEvent);

	((MainCalendarManagerWindow*)BaseWindowManager::getSingleton().getWindow("CalendarManagerWindow"))->getCurrentCalendarWindow()->refreshEvent();
	return true;
}

bool EventTagWindow::handle_editEvent(const CEGUI::EventArgs& args)
{
	static_cast<EventEditWindow*>(BaseWindowManager::getSingleton().getWindow("EditEventWindow"))
		->showWindow(_relatedEvent);
	
	return true;
}